package com.kx.smsplantform.strategy.impl;

import com.kx.smsplantform.client.CacheClient;
import com.kx.smsplantform.strategy.Strategy;
import com.kx.smsplantform.utils.IKAnalyzerUtil;
import com.kx.smsplantform.utils.PushMessage2Queue;
import com.kx.smsplatform.common.constants.CacheConstants;
import com.kx.smsplatform.common.constants.StrategyConstants;
import com.kx.smsplatform.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 脏词处理策略
 *
 * @Author Sun
 * @Date 2021/7/2 11:36
 */

@Slf4j
@Service(value = "dirtyWord")
public class DirtyWordStrategy implements Strategy {

    @Autowired
    private CacheClient client;

    @Autowired
    private PushMessage2Queue pushMessage2Queue;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) throws IOException {
        log.info("【策略模块-脏词校验】 脏词校验策略开始执行！！");


        //将用户传递过来的短信内容利用ik分词器进行分词，存在另一个set集合中,然后存入redis
        String messageContent = standardSubmit.getMessageContent();
        List<String> stringList = IKAnalyzerUtil.cut(messageContent);
        client.sAdd("DIRTY_WORDS1:", stringList.toArray(new String[stringList.size()]));

        //在缓存模块将两个set集合取交集，只要有交集就说明有敏感词，返回false
        Set<String> set = client.sIntersect(CacheConstants.CACHE_PREFIX_DIRTY_WORDS, "DIRTY_WORDS1:");
        if(set.size()>0){//说明有交集
            log.info("【策略模块-脏词校验】 短信内容中包含脏词，脏词为set={}",set);
            pushMessage2Queue.sendLogMessage(standardSubmit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            pushMessage2Queue.pushReportMessage(standardSubmit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            return false;
        }
        log.info("【策略模块-脏词校验】 脏词校验策略执行结束！！");
        return true;
    }
}
